var _set = require("./../../API/set.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    page:1,
    loadend: !1,
    isShua:1,
    work:0,
    is_choose:0,
    shops:[],
    schools:[],
    school_ids:'',
    shop_ids:'',
    deviceType:app.globalData.deviceType
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoadFun:function(options){
    this.getList();
    this.chooseOrder();
    this.getUserInfo();
    this.setData({
      deviceType:app.globalData.deviceType
    })
  },
  onLoad: function () {
    
  },
  getUserInfo:function(){
    var self = this;
    (0, _set.getUserInfo)().then(function(t) {
        self.setData({
          userInfo:t.data,
          work:t.data.is_work
        })
        wx.setStorage({
          key:"userInfo",
          data:t.data
        });
        app.globalData.userInfo = t.data;
    }).catch(function(t) {
      return app.Tips({
          title: t
      });
  });
  },
  toWork:function(){
    var self = this;
    (0, _set.toWork)().then(function(t) {
        self.setData({
          work:!self.data.work
        })
    }).catch(function(t) {
      return app.Tips({
          title: t
      });
  });
  },
  robOrder:function(e){
    var self = this;
    wx.showModal({
      title: '提示',
      content: '是否确认抢单',
      success (res) {
        if (res.confirm) {
          (0, _set.toReceive)({
            order_id:e.currentTarget.dataset.id
          }).then(function(t) {
              self.setData({
                list:[],
                page:1,
                loadend: !1,
              });
              self.getList();
              return app.Tips({
                title: '操作成功'
            });
          }).catch(function(t) {
            self.setData({
              list:[],
              page:1,
              loadend: !1,
            });
            self.getList();
            return app.Tips({
                title: t
            });
        });
      } else if (res.cancel) {
        // console.log('用户点击取消')
      }
    }
  })
  },
  refresh:function(){
    this.setData({
      list:[],
      page:1,
      loadend: !1,
      isShua:0,
      is_choose:0
    });
    wx.showLoading({
      title: '加载中...',
    })
    this.getList();
  },
  getList:function(){
    var self = this;
    !self.data.loadend&&(0, _set.getOrderHallList)({
        school_id:self.data.school_ids,
        shop_id:self.data.shop_ids,
        page: self.data.page
    }).then(function(t) {
      wx.hideLoading();
        var o = t.data.data || [], a = o.length < t.data.per_page;
        self.data.list = app.SplitArray(o, self.data.list), self.setData({
          list: self.data.list,
          loadend: a,
          page: self.data.page + 1
        });
    }).catch(function(t) {
      wx.hideLoading();
      return app.Tips({
          title: t
      });
  });
  },
  priveImg:function(e){
    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.url] // 需要预览的图片http链接列表
    })
  },
  toChoose:function(){
    this.setData({
      is_choose:1
    });
    
  },
  onClose:function(){
    this.setData({
      is_choose:0
    });
  },
  chooseOrder:function(){
    var self = this;
     _set.getChooseShopsList().then(function(t) {
      wx.hideLoading();
       self.setData({
          shops: t.data.shops,
          schools:t.data.schools
        });
    }).catch(function(t) {
      wx.hideLoading();
      return app.Tips({
          title: t
      });
    });
  },
  toSelectSchool:function(e){
    var idx = e.currentTarget.dataset.school_index;
    var schools = this.data.schools;
    if(schools[idx].check){
      schools[idx].check = false;
    }else{
      schools[idx].check = true;
    }
    this.setData({
      schools:schools
    })
  },
  toSelectShop:function(e){
    var idx = e.currentTarget.dataset.shop_index;
    var shops = this.data.shops;
    if(shops[idx].check){
      shops[idx].check = false;
    }else{
      shops[idx].check = true;
    }
    this.setData({
      shops:shops
    })
  },
  toReset:function(){
    var shop_list = this.data.shops,school_list = this.data.schools,shops=[],schools=[];
    for(let i=0;i<school_list.length;i++){
        school_list[i].check = false;
    };
    for(let i=0;i<shop_list.length;i++){
        shop_list[i].check = false;
    };
    this.setData({
      shops:shop_list,
      schools:school_list
    });
  },
  toSureSelect:function(){
    var shop_list = this.data.shops,school_list = this.data.schools,shops=[],schools=[];
    for(let i=0;i<school_list.length;i++){
      if(school_list[i].check){
        schools.push(school_list[i].school_id)
      }
    };
    for(let i=0;i<shop_list.length;i++){
      if(shop_list[i].check){
        shops.push(shop_list[i].shop_id)
      }
    };
    this.setData({
      shop_ids:shops.toString(),
      school_ids:schools.toString()
    });

    this.refresh()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list:[],
      page:1,
      loadend: !1,
    })
    this.refresh();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})